/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.books.service.aop;

import com.books.service.LoginService;
import com.books.struts.action.LoginAction;
import com.books.struts.form.LoginActionForm;
import java.lang.reflect.Method;
import java.util.Enumeration;
import javax.servlet.http.HttpServletRequest;
import org.springframework.aop.MethodBeforeAdvice;
import org.apache.log4j.Logger;

/**
 *
 * @author liangu
 */
public class LoginLogService implements MethodBeforeAdvice{

    private Logger log = Logger.getLogger(LoginLogService.class);

    public void before(Method method, Object[] obj, Object target) throws Throwable {

      //For the arguments of Execute, there are three parameters:ActionMapping mapping,ActionForm form,
      //HttpServletRequest request, HttpServletResponse response

      LoginAction loginAct = (LoginAction)target;
      LoginActionForm loginForm = (LoginActionForm)obj[1];
      HttpServletRequest request = (HttpServletRequest)obj[2];

       Enumeration headerNames = request.getHeaderNames();
       StringBuffer userinfo = new StringBuffer();
       while (headerNames.hasMoreElements())
       {
           String header = (String)headerNames.nextElement();
           userinfo.append(header+":"+request.getHeader(header)+";");
       }


      log.info("User : " + loginForm.getName()+" from "
               + request.getLocalAddr()+ " with " + userinfo.toString());
        
    }

}
